﻿using DevExpress.XtraEditors;
using MDK.Device;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform
{
    public partial class frmOperationTube : BaseForm
    {
        public int _Position { get; set; }

        private int index = 0;
        public frmOperationTube()
        {
            InitializeComponent();
        }

        public frmOperationTube(int Position) :this()
        {
            _Position = Position;

            palletcode.Text = App.flow.PosModel.DesktopPositions[_Position].Pallet.ActiveBarcode;
            var type = App.flow.PosModel.DesktopPositions[_Position].Pallet.Type;

            switch (type)
            {
                case TubeTypes.Meimian:
                    tubetype.SelectedIndex = 0;
                    break;
                case TubeTypes.Hesuan:
                    tubetype.SelectedIndex = 1;
                    break;
            }

            InitTubeStatus();

            splashScreenManager1.ShowWaitForm();
            if (App.flow.BeginManaulHandle())
            {
                splashScreenManager1.CloseWaitForm();
            }
        }

        public void InitTubeStatus()
        {
            App.flow.PosModel.DesktopPositions[_Position].Pallet = new Device.Pallet(){State = PalletStatus.WaitToFreezer};
            App.flow.PosModel.DesktopPositions[_Position].Pallet.Tubes = Enumerable.Range(0, 20).Select(t => new Tube() { Type = TubeTypes.None }).ToArray();
            var pallet = App.flow.PosModel.DesktopPositions[_Position].Pallet;
            if (pallet != null)
            {
                var tubes = pallet.Tubes;
                //工位
                for (int i = 0; i < tubes.Count(); i++)
                {
                    if (tubes[i] != null)
                    {
                        if (string.IsNullOrEmpty(tubes[i].Barcode))
                        {
                            var control = ((PictureEdit)(this.Controls.Find("Tube" + i, true)[0]));
                            control.Image = MDK.Winform.Properties.Resources.无条码;
                        }
                    }
                }
            }
        }
        private void Tube_Click(object sender, EventArgs e)
        {
            var controlname = (sender as PictureEdit).Name;
            int.TryParse(controlname.Substring(4, controlname.Length - 4), out index);
            tubecode.Text = App.flow.PosModel.DesktopPositions[_Position].Pallet.Tubes[index].Barcode;
        }

        private void btnModifyPallet_Click(object sender, EventArgs e)
        {
            App.flow.PosModel.DesktopPositions[_Position].Pallet.ActiveBarcode = palletcode.Text;
            TubeTypes type = TubeTypes.None;
            switch (tubetype.SelectedIndex)
            {
                case 0:
                    type = TubeTypes.Meimian;
                    break;
                case 1:
                    type = TubeTypes.Hesuan;
                    break;
            }

            App.flow.PosModel.DesktopPositions[_Position].Pallet.Tubes[index].Type = type;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            App.flow.PosModel.DesktopPositions[_Position].Pallet.Tubes[index].Barcode = tubecode.Text;
            var control = ((PictureEdit)(this.Controls.Find("Tube" + index, true)[0]));
            control.Image = MDK.Winform.Properties.Resources.正常;
        }

        private void frmOperationTube_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = FastMessageBox.Question("确认已经补完所有试管");
            if (App.flow.PosModel.DesktopPositions[_Position].Pallet.NoBarcodeCount == 0  
                && App.flow.PosModel.DesktopPositions[_Position].Pallet.Type !=  TubeTypes.Unknown 
                && !string.IsNullOrEmpty( App.flow.PosModel.DesktopPositions[_Position].Pallet.ActiveBarcode) && dr == DialogResult.OK)
            {
                App.flow.EndManaulHandle();
                e.Cancel = false;
            }
            else
                e.Cancel = true;
        }
    }
}
